import React from 'react';
import router from 'umi/router';

import { connect } from 'dva';

import { Card, Divider, Button,  Modal } from 'antd';

import DescriptionList from '@/components/DescriptionList';
const { Description } = DescriptionList;

const formPath = '/AccountConfig/ProductProductForm';
const listPath = '/AccountConfig/ProductProducts';



@connect(({ productProduct }) => ({ productProduct }))
class Profile extends React.Component {
  state = {
    visibleDel: false,
  }


  id = 0

  async componentDidMount() {
    const { location: {query: { id } } } = this.props;
    this.id = Number(id)
    if (this.id) {
      const { dispatch } = this.props;
      dispatch({
        type: 'productProduct/view',
        payload:  { id: this.id }
      });
    }
  }


  gotoForm() {
    if(this.id){
      router.push(`${formPath}?id=${this.id}`);
    }
  }


  showDelete() {
    this.setState({
      visibleDel: true
    })

  }

  handleCancel(){
    this.setState({
      visibleDel: false,
    })
  }

  async HandleDelete(){
    if (this.id) {
      const { dispatch } = this.props;
      await dispatch({
        type: 'productProduct/unlink',
        payload:  { id: this.id }
      });

      const { productProduct } = this.props;
      if( productProduct.id ){
      }
      else{
        router.push(listPath);

      }
    }
  }

  render() {
    const { productProduct: { record } } = this.props;

    const {  visibleDel } = this.state


    return (
      <Card bordered={false}>

        <DescriptionList size="large" title='操作控制' style={{ marginBottom: 32 }}>
          <Description >
            <Button onClick={()=>this.gotoForm()}>编辑</Button>
          </Description>
          <Description >
            <Button onClick={()=>this.showDelete()}>删除</Button>
          </Description>

        </DescriptionList>

        <Modal title='删除'
          visible={visibleDel}
          onOk={()=>this.HandleDelete()}
          onCancel={()=>this.handleCancel()}
        >
          确认删除?

        </Modal>


        <DescriptionList size="large" title='基本信息' style={{ marginBottom: 32 }}>
          <Description term='名称' >{record.name}</Description>
          <Description term='代码' >{record.default_code}</Description>
          <Description term='费用科目' >{record.property_account_expense_id && record.property_account_expense_id.name}</Description>
          <Description term='收益科目' >{record.property_account_income_id && record.property_account_income_id.name}</Description>
        </DescriptionList>
        <Divider style={{ marginBottom: 32 }} />
      </Card>
    );
  }
}

export default Profile;

